package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.ResultCode;
import com.example.dao.StudentInfoDao;
import com.example.dao.TeacherInfoDao;
import com.example.dao.XueyuanInfoDao;
import com.example.entity.Account;
import com.example.entity.StudentInfo;
import com.example.entity.TeacherInfo;
import com.example.entity.XueYuanInfo;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 涛某
 * @version 1.0
 * @date 2024/3/21 13:53
 */
@Service
public class StudentInfoService {

    @Autowired
    private StudentInfoDao studentInfoDao;
    @Autowired
    private XueyuanInfoDao xueyuanInfoDao;
    public void update(StudentInfo studentInfo) {
        studentInfoDao.update(studentInfo);
    }

    public void add(StudentInfo studentInfo) {
        StudentInfo info = studentInfoDao.FindByName(studentInfo.getName());
        //先查看用户名是否已经存在
        if (ObjectUtil.isNotEmpty(info)){
            throw new CustomException(ResultCode.USER_EXIST_ERROR);
        }
        //如果不存在的话就赋予一个初始密码
        if (ObjectUtil.isEmpty(studentInfo.getPassword())){
            studentInfo.setPassword("123456");
        }
        studentInfoDao.add(studentInfo);

    }

    public List<StudentInfo> findAll() {
     /*   List<StudentInfo> list = studentInfoDao.findAll();
        for (StudentInfo studentInfo : list) {
            if (ObjectUtil.isNotEmpty(studentInfo.getXueyuanid())) {
                XueYuanInfo xueYuanInfo = xueyuanInfoDao.selectByPrimaryKey(studentInfo.getXueyuanid());
                studentInfo.setXueyuanname(xueYuanInfo.getName());
            }
        }
        return list;*/
       List<StudentInfo> list2 =  studentInfoDao.findAllJoinxueyuan();
       return list2;
    }

    public void deleteById(Long id) {
        studentInfoDao.deleteById(id);
    }

    public PageInfo<StudentInfo> findPage(Integer pageNum, Integer pageSize) {

        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        //查询会根据pageNum pageSize 来查询相应数据
        List<StudentInfo> infos = studentInfoDao.findAllJoinxueyuan();
        return PageInfo.of(infos);
    }

    public PageInfo<StudentInfo> findPageName(Integer pageNum, Integer pageSize, String name) {
        //开始分页
        PageHelper.startPage(pageNum,pageSize);
        List<StudentInfo> infos = studentInfoDao.findByNamePage(name);
        return PageInfo.of(infos);
    }

    public Account login(String name, String password) {
        StudentInfo studentInfo = studentInfoDao.findByNameAndPassowrd(name, password);
        if (ObjectUtil.isEmpty(studentInfo)){
            throw new CustomException("1","用户名或者密码输入错误");
        }
        return studentInfo;

    }

    public StudentInfo FindById(Long id) {
        return  studentInfoDao.FindById(id);
    }

    public void register(StudentInfo studentInfo) {
        //在service里层里对输入进来的用户进行校验
        //先对用户名的名字进行一个检查查看用户名是否被占用


        StudentInfo info = studentInfoDao.FindByName(studentInfo.getName());
        if (ObjectUtil.isNotEmpty(info)){
            throw new CustomException(ResultCode.USER_EXIST_ERROR);
        }else {
          studentInfoDao.insertSelective(studentInfo);
        }


        //校验通过了在调用方法把数据添加到数据库中

    }

}
